﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace System.ComponentModel.Composition
{
#if !SILVERLIGHT
    [Serializable]
#endif
    public class CompositionException : Exception
    {
        private string _message;
        private Collection<CompositionIssue> _issues = new Collection<CompositionIssue>();

        public CompositionException() : this(Enumerable.Empty<CompositionIssue>()) { }

        public CompositionException(string message) : this(message, null) { }

        public CompositionException(string message, Exception innerException)
            : base(message, innerException)
        {
            this._message = message;
            this.Issues = new ReadOnlyCollection<CompositionIssue>(this._issues);
        }

        public CompositionException(IEnumerable<CompositionIssue> issues)
        {
            issues = issues ?? Enumerable.Empty<CompositionIssue>();
            this._issues = new Collection<CompositionIssue>(issues.ToList());
            this.Issues = new ReadOnlyCollection<CompositionIssue>(this._issues);
        }

#if !SILVERLIGHT
        protected CompositionException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.Issues = (ReadOnlyCollection<CompositionIssue>)info.GetValue("Issues", typeof(ReadOnlyCollection<CompositionIssue>));
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Issues", this.Issues, typeof(ReadOnlyCollection<CompositionIssue>));
            base.GetObjectData(info, context);
        }
#endif //!SILVERLIGHT

        protected void AddIssue(CompositionIssue issue)
        {
            this._issues.Add(issue);
        }

        public override string Message
        {
            get
            {
                if (string.IsNullOrEmpty(this._message))
                {
                    StringBuilder builder = new StringBuilder();

                    // The composition produced {0} composition issues. The first few issues of severity level 'error' are provided below. Review the Issues collection for more detailed information.
                    builder.AppendFormat(
                        CultureInfo.CurrentCulture,
                        Strings.CompositionExceptionMessage,
                        this.Issues.Count);

                    builder.AppendLine();
                    foreach (var issue in this.Issues.Where(i => i.Severity == CompositionIssueSeverity.Error).Take(5))
                    {
                        builder.AppendLine(issue.ToString());
                    }

                    this._message = builder.ToString();
                }
                return this._message;
            }
        }

        public ReadOnlyCollection<CompositionIssue> Issues { get; private set; }
    }
}
